package com.mkyong.common.controller;

import javax.transaction.Transactional;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

import com.mkyong.common.persistence.Employee;

@Service
public class ManageEmployee {

	@Autowired
	private SessionFactory sessionFactory;

	public Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public Integer addEmployee(Employee e) {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		Integer employeeID = null;

		try {
			tx = session.beginTransaction();
			employeeID = (Integer) session.save(e);
			tx.commit();
		} catch (HibernateException ex) {
			if (tx != null)
				tx.rollback();
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return employeeID;
	}
	
	 /* Method to  READ all the employees */
	   public List<Employee> listEmployees( ){
	      Session session = sessionFactory.openSession();
	      Transaction tx = null;
	      List<Employee> employees = null; 
	      try{
	         tx = session.beginTransaction();
	         employees = (List<Employee>)session.createQuery("FROM Employee").list();
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	         return null;
	      }finally {
	         session.close(); 
	      }
	      return employees;
	   }
	   
	   /* Method to READ an employee*/
	   public Employee getEmployee(int id){
	      Session session = sessionFactory.openSession();
	      Transaction tx = null;
	      Employee employee = null;
	      try{
	         tx = session.beginTransaction();
	         employee = (Employee)session.get(Employee.class, id); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return employee;
	   }
	   
	   /* Method to UPDATE an employee */
	   public void updateEmployee(Employee em){
	      Session session = sessionFactory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Employee employee = 
	                    (Employee)session.get(Employee.class, em.getId());
	         employee.setFirstName(em.getFirstName());
	         employee.setLastName(em.getLastName());
	         employee.setSalary( em.getSalary() );
			 session.update(employee); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	   }
	   
	   /* Method to DELETE employee */
	   public boolean deleteEmployee(int id){
	      Session session = sessionFactory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Employee employee = 
	                    (Employee)session.get(Employee.class, id);
	         if(employee == null)
	        	 return false;
			 session.delete(employee); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	         return false;
	      }catch (NullPointerException nlp){
	    	  if (tx!=null) tx.rollback();
		         nlp.printStackTrace(); 
		         return false;
	      }
	      finally {
	         session.close(); 
	      }
	      return true;
	   }

	/*@Transactional
	public Integer addEmployeeWithTransactionManager(Employee e) {
		return (Integer) sessionFactory.getCurrentSession().save(e);

	}*/

}
